local BajiRespawn = SPAWN:New("Baji")
local Baji_WWII = SPAWN:New("Baji WWII")
local BATON_Flag = USERFLAG:New("baton")

function setFlag( data )
	trigger.action.setUserFlag(data.flagname, data.flagval)
end

function respawnTGT( typename )
	local tgttype = typename["val"]

	if tgttype == 6 then -- WWII
		Baji_WWII:ReSpawn()
		MessageToAll("=============\n" ..
		"二战靶机已刷新。\n地区: 塞纳基·科尔奇以南山区\n" ..
		"=============", 10, "训练项目")
	elseif tgttype == 4 then --F86
		mooseRespawn( "ColdWar" )
		trigger.action.outText("冷战空战机已刷新。 ", 10, 0, false)
		trigger.action.outText("地区: 苏呼米空域", 10, 0, false)
	elseif tgttype == 5 then
		mooseRespawn( "RED Helo" )
		trigger.action.outText("直升机靶机已刷新。 ", 10, 0, false)
		trigger.action.outText("地区: 库塔伊西对地区", 10, 0, false)
	elseif tgttype == "groundBTRs" then -- 库塔伊西固定车队目标
		mooseRespawn( "AG-1" )
		trigger.action.outText("库塔伊西方向的固定装甲车组目标 已重新部署", 10, 0, false)
	elseif tgttype == "groundbase" then -- 库塔伊西基地
		mooseRespawn( "BASE" )
		trigger.action.outText("库塔伊西方向的红方基地 已重新部署", 10, 0, false)
	elseif tgttype == "refuel1" then -- KC135MPRS
		mooseRespawn( "Fuel-1" )
		trigger.action.outText("空中加油机S-3B 已重新部署", 10, 0, false)
	elseif tgttype == "refuel2" then -- KC135
		mooseRespawn( "Fuel-2" )
		trigger.action.outText("空中加油机KC-135 已重新部署", 10, 0, false)
	elseif tgttype == "refuel3" then -- KC135MPRS mission
		mooseRespawn( "Fuel-3" )
		trigger.action.outText("纵向应变任务区域加油机 KC-135 已重新部署", 10, 0, false)
	elseif tgttype == "refuel4" then
		mooseRespawn("Fuel-4")
		trigger.action.outText("纵向应变任务区域加油机 S-3B 已重新部署", 10, 0, false)
	elseif tgttype == "jtac" then -- JTAC
		mooseRespawn( "JTAC-1" )
		trigger.action.outText("库塔伊西方向JTAC 已重新部署", 10, 0, false)
	elseif tgttype == "aew" then -- 预警机
		mooseRespawn( "Yujin" )
		trigger.action.outText("预警机 已重新部署. 频道261.000AM", 10, 0, false)
	elseif tgttype == "batonred" then
		BATON_Flag:Set(1)
		-- setFlag( {flagname = "baton", flagval = 1} )
		trigger.action.outText("巴统战区 红方已经重新部署", 10, 0, false)
	elseif tgttype == "batonblue" then
		BATON_Flag:Set(2)
		-- setFlag( {flagname = "baton", flagval = 2} )
		trigger.action.outText("巴统战区 蓝方已经重新部署", 10, 0, false)
	end
end


function mooseRespawn( groupname )
	if groupname ~= nil then
		local plane = SPAWN:New( tostring(groupname) )
		plane:Spawn()
	end
end

function tgtRespawn( group )
	if group ~= nil then
		local groupname = group["name"]
		local plane = SPAWN:New( groupname )
		plane:Spawn()
	end
end

-- 清理靶机函数
function BajiClean()
	local bajigroups = SET_GROUP:New():FilterPrefixes("Baji"):FilterStart()
	bajigroups:ForEachGroupAlive(function( cleanupgroup )
				local tempgroup = Group.getByName(cleanupgroup:GetName())
				if tempgroup ~= nil then
					tempgroup:destroy()
				end
			end
		)
end

-- 靶机重生函数开始
function BajiSpawn()
	BajiClean()
	local planenumber = trigger.misc.getUserFlag("bajinumber") -- 靶机数量flag
	local planetype = trigger.misc.getUserFlag("bajitype") -- 靶机类型flag. 1为导弹, 2为机炮

	bajiReportMsg = "靶机数量: " .. tostring(planenumber) .."\n"
	if planetype == 1 then
		PlanePrefixes = "Baji Template #" -- 导弹靶机template
		bajiReportMsg = bajiReportMsg .. "靶机种类: 导弹"
	elseif planetype == 2 then
		PlanePrefixes = "Baji Template Gun #" -- 机炮靶机template
		bajiReportMsg = bajiReportMsg .. "靶机种类: 机炮"
	end

	MESSAGE:New(bajiReportMsg, 10, "当前的靶机设置", false):ToAll()

	BajiRespawn:InitLimit(planenumber, planenumber)
	BajiRespawn:InitRandomizeTemplatePrefixes(PlanePrefixes)
	BajiRespawn:OnSpawnGroup( function(group)
					MESSAGE:New("靶机已经刷新, 机型:" .. tostring(group:GetTypeName()) .. 
						".\n" .. bajiReportMsg, 10, "空战训练生成", false):ToAll()
					
				end
				)
	BajiRespawn:SpawnScheduled(4, 0.15)
	local spawnedplane = BajiRespawn:ReSpawn()

	if spawnedplane == nil then
		MESSAGE:New("未检测到刷新出的靶机！！！！", 20, "DEBUG", true):ToAll()
	end
end

function setBajiNumber( number )
	trigger.action.setUserFlag("bajinumber", number["val"])
	MESSAGE:New("靶机的数量调整为: " .. tostring(number["val"]), 10, "空战训练设置", false):ToAll()
end

function setBajiType( type )
	trigger.action.setUserFlag("bajitype", type["val"])
	if type["val"] == 1 then
		msg = "靶机的武器设置为: 导弹"
	else
		msg = "靶机的武器设置为: 机炮"
	end
	MESSAGE:New(msg, 10, "空战训练设置", false):ToAll()
end
-- 靶机重生函数结束

-- 通讯菜单开始
local TrainMenu = missionCommands.addSubMenu("训练菜单")

local AirTGT = missionCommands.addSubMenu("空中目标", TrainMenu)
local a2atrain = missionCommands.addSubMenu("空站训练设置", AirTGT)

missionCommands.addCommand("-- 刷新空战训练用机", AirTGT, BajiSpawn)
missionCommands.addCommand("-- 清理空战训练用机", AirTGT, BajiClean)
local bajinumberMenu = missionCommands.addSubMenu("设置靶机数量", a2atrain)
missionCommands.addCommand("-- 1", bajinumberMenu, setBajiNumber, { val = 1 })
missionCommands.addCommand("-- 2", bajinumberMenu, setBajiNumber, { val = 2 })
missionCommands.addCommand("-- 3", bajinumberMenu, setBajiNumber, { val = 3 })
missionCommands.addCommand("-- 4", bajinumberMenu, setBajiNumber, { val = 4 })

local bajitypeMenu = missionCommands.addSubMenu("设置靶机武器", a2atrain)
missionCommands.addCommand("-- 导弹", bajitypeMenu, setBajiType, { val = 1 })
missionCommands.addCommand("-- 机炮", bajitypeMenu, setBajiType, { val = 2 })

missionCommands.addCommand("-- 冷战空战 F-86 导弹", AirTGT, respawnTGT, {val = 4})
missionCommands.addCommand("-- 靶机 直升机Ah64D 无挂载", AirTGT, respawnTGT, {val = 5})
missionCommands.addCommand("-- 二战 空战", AirTGT, respawnTGT, {val = 6})


local Ground = missionCommands.addSubMenu("地面目标", TrainMenu)
missionCommands.addCommand("地面固定车队-库塔伊西方向", Ground, respawnTGT, {val = "groundBTRs"})
missionCommands.addCommand("地面固定基地-库塔伊西方向", Ground, respawnTGT, {val = "groundbase"})

local Refuel = missionCommands.addSubMenu("空中加油", TrainMenu)
missionCommands.addCommand("软管 S-3B", Refuel, respawnTGT, {val = "refuel1"})
missionCommands.addCommand("硬管 KC-135", Refuel, respawnTGT, {val = "refuel2"})

local Ally = missionCommands.addSubMenu("友军部署", TrainMenu)
missionCommands.addCommand("库塔伊西方向JTAC 重新部署", Ally, respawnTGT, {val = "jtac"})
missionCommands.addCommand("预警机 重新部署", Ally, respawnTGT, {val = "aew"})

local Baton = missionCommands.addSubMenu("巴统联合武装战区", TrainMenu)
missionCommands.addCommand("红方重新部署", Baton, respawnTGT, {val = "batonred"})
missionCommands.addCommand("蓝方重新部署", Baton, respawnTGT, {val = "batonblue"})
